package farkle;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import farkle.Die;
import farkle.FarkleApp;
//Comments


public class FarkleFrame extends JFrame
{
    
    //Initializes the needed objects and values to construct the Frame
    public static Die[] Dice = new Die[6];
    JButton rollBut = new JButton("Roll Die");
    JButton[] holdBut = new JButton[6];
    JButton bankBut = new JButton("Bank");
    String score = "0";
    JTextField scoreBox = new JTextField(5);
    Font bigFont = new Font("Arial", Font.BOLD, 16);
    Font smallFont = new Font("Arial", Font.BOLD, 10);
    final int WIDTH = 650;
    final int HEIGHT = 175;
    final int HOLDBUTWIDTH = 60;
    final int HOLDBUTHEIGHT = 20;
    public JPanel panel1 = new JPanel(new FlowLayout());
    public JPanel panel2 = new JPanel(new FlowLayout());
    int hDice = 0;
    int[] heldDicee;
    int hMarker = 0;
    boolean[] holdDice = {false,false,false,false,false,false};
    
    
    
    
    
    

   FarkleFrame()
   {
       super("FARKLE!");
       //Creates an array of dice objects with dice containing the value of 1
       for(int a = 0; a < Dice.length; a++)
           Dice[a] = new Die(1);
       setSize(WIDTH,HEIGHT);               
       setLayout(new FlowLayout());
       panel1.setPreferredSize(new Dimension(450, 175));
       panel2.setPreferredSize(new Dimension(100, 175));
       
       
       
       rollBut.setFont(bigFont);
       bankBut.setFont(bigFont);
       scoreBox.setFont(bigFont);
       bankBut.addActionListener(new BankListener());

       for(int b = 0; b < Dice.length; b++)
       {
           panel1.add(Dice[b]);
           Dice[b].addMouseListener(new HoldListener());
           
       }
       scoreBox.setSize(new Dimension(75,40));
       panel2.add(bankBut);
       panel2.add(rollBut);
       panel2.add(scoreBox);
       
       
       add(panel1);
       add(panel2);
       
        
       scoreBox.setHorizontalAlignment(JTextField.CENTER);
       
       rollBut.addActionListener(new RollListener());
       this.addWindowListener(new WindowAdapter(){
            @Override
       public void windowClosing(WindowEvent e){
           System.exit(0);
            } 
       });
   }   
   
   public class RollListener implements ActionListener
   {
    public void actionPerformed(ActionEvent e)
   {
       for(int c = 0;c < Dice.length; c++)
       {
           if(holdDice[c] == false)
               Dice[c].roll();
           
       }
                
       
   }
   }

   public class BankListener implements ActionListener
   {
       public void actionPerformed(ActionEvent e)
       {
           for(int g = 0; g < Dice.length; g++)
           {
               if(holdDice[g] == true)
                   hDice += 1;
           }
           int[] heldDicee = new int[hDice];
           for(int h = 0; h < Dice.length; h++)
           {
               if(holdDice[h] == true)
               {
                   heldDicee[hMarker] = Dice[h].getValue();
                   hMarker += 1;
               }
           }
           
           score = Integer.toString(FarkleApp.score(heldDicee));
           scoreBox.setText(score);
           
           
           
                      
       }
   }
   
   public class HoldListener implements MouseListener {

        public void mouseClicked(MouseEvent e) {
            for (int i = 0; i < Dice.length; i++){
                if (e.getComponent() == Dice[i] && holdDice[i] == false){
                    holdDice[i] = true;
                    Dice[i].setBackground(Color.YELLOW);
                }
                else if (e.getComponent() == Dice[i] && holdDice[i] == true){
                    holdDice[i] = false;
                    Dice[i].setBackground(Color.WHITE);
                }
            }
        }

        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
       
   }
   
  //TODO Need to add handler method for clicking on the dice.
  
   
    
}


    
   
    
    


